import CryptoJS from "crypto-js";
import dayjs from 'dayjs';

export default function encrypt(data) {
    // 生成随机 IV
    const iv = CryptoJS.lib.WordArray.random(16);
    // 加密
    const encrypted = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse("0125abc6789def34"), {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
    });

    // 返回 Base64 格式的加密数据
    return iv.toString(CryptoJS.enc.Base64) + ":" + encrypted.ciphertext.toString(CryptoJS.enc.Base64);
}


export function formatTime(timestamp: string) {
    return dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss');
}

// 防抖
export function debounceTime(delay) {
    let timer = null;
    return function (fn) {
        clearTimeout(timer);
        timer = setTimeout(() => {
            fn.apply(this, arguments);
        }, delay);
    };
}

export function fromTime() {
    const now = new Date();
    now.setHours(0, 0, 0, 0); // 设置时间为当天的 00:00:00
    const seconds = Math.floor(now.getTime()); // 转换为秒级时间戳
    return seconds
}

export function formatTimestamp(timestamp) {
    const date = new Date(Number(timestamp)); // 将秒级时间戳转换为Date对象

    const year = date.getFullYear();
    const month = date.getMonth() + 1; // 月份从0开始，所以要加1
    const day = date.getDate();

    if (fromTime() == timestamp) {
        return `今天`;
    }

    return `${year}年${month}月${day}日`;
}

export function lexicalEditParse(textParam) {
    if (!textParam) {
        return ""
    }
    const jsonData = JSON.parse(textParam)
    const data = jsonData["root"].children[0].children.map(item => item?.content?.value || item?.text)
    console.log(data)
    return data.join('')
}